/*
 * @Descripttion:
 * @version: V1.0
 * @Author: ZYP  2305592180@qq.com
 * @Date: 2025-05-27 09:50:48
 * @LastEditors: ZYP
 * @LastEditTime: 2025-11-10 10:53:57
 */

#ifndef __MMC_H
#define __MMC_H

#include "freertos/FreeRTOS.h"
#include "sdkconfig.h"
#include "sdmmc_cmd.h"
#include "driver/sdmmc_host.h"
#include "esp_log.h"
#include "sdmmc_common.h"
#include "esp_err.h"

#define EMMC_RST_GPIO GPIO_NUM_12
#define RST_PULSE_WIDTH_MS 0.01    // Reset pulse width, 0.01ms  10us

#define MMC_SEND_WRITE_PROT 30
#define SEND_WRITE_PROT_TYPE 31

#define ROL(x, y, z) (((x) << (y)) | ((x) >> (z - (y))))
#define LE16_TO_BE(i) ((ROL((i), 8, 16) & 0xFF00) | (ROL((i), 8, 16) & 0x00FF))
#define LE32_TO_BE(i) ((ROL((i), 24, 32) & 0xFF00FF00) | (ROL((i), 8, 32) & 0x00FF00FF))

#define MMC_PROGRAM_CID_CSD_START_FLAG 0X100600BA
#define MMC_PROGRAM_CID_CSD_FREAM_DATASIZE 21
#define IS_SUPOORT_PROGRAM_CID_CSD 1
#define MMC_PROGRAM_CID 26
#define MMC_PROGRAM_CSD 27
#define MMC_PROGRAM_CID_CSD_DATASIZE 16

#define MMC_RESPONCE_TYPE_R1 0X01
#define MMC_RESPONCE_TYPE_R2 0X02
#define MMC_RESPONCE_TYPE_R3 0X03
#define MMC_RESPONCE_BYTE (16)

#define SEND_OP_COND_MAX_ENTRY 50
#define SEND_OP_COND_NOREADY 0X40FF8080
#define SEND_OP_COND_READY 0XC0FF8080

#define MMC_WRITE_SEDND 1
#define SWITCH_HIGH_SHIFTBIT 0X00ff0000
#define SWITCH_BUS_WTCH_SHIFTBIT 0X00b70000
#define SWITCH_DDR_SHIFTBIT 0X00000400
#define BUS_WITH_SHIFTBIT 0x300

#define SWITCH_HIGH_SPEED 0x03B90100
#define SWITCH_HS200 0x03B90200
#define SWITCH_HS400 0x03B90300

#define HIGH_SPEED_VALUE 0x03B90100
#define DDR_4BIT_VALUE 0x03B70500
#define SDR_8BIT_VALUE 0x03B70200
#define SDR_4BIT_VALUE 0x03B70100
#define SDR_1BIT_VALUE 0x03B70000

#define MMMC_SCHEME_BUSTEST 1
#define MMC_SET_DSR 4
#define MMC_SLEEP_AWAK 5
#define MMC_GO_INCTIVE 15
#define MMC_CMD_BUS_TEST_W 19
#define MMC_CMD_BUS_TEST_R 14
#define MMC_CMD_APP_CMD_2 56
#define MMC_POWER_SET (0xFF)
#define ESP_POWER_SET (0xFE)
#define Hard_Reset_N (0xFD)

#define IS_NEED_HARD_CODE 0

#define MMC_APP_CMD61 61
#define MMC_APP_CMD61_PARAMTER 0x20464C4C
#define MMC_APP_CMD60 60
#define MMC_GO_IDLE_STATE_PARAMETER 0x40FF8080
#define MMC_PARME_ZEARO 0x00000000

#define ENABLE_CMD60_CMD61 1

#define ENABLE_4BYT_ALIGNM_EDMA_WRITE 1
#define ENABLE_NOT_ALIGNM_EDMA_WRITE 0

#define ENABLE_FORMAT_STRING_TYPE 1
#define NOT_ENABLE_FORMAT_STRING_TYPE 0
#define IS_ENABLE_8BIT_50M 0
#define IS_ENABLE_SDR_8BIT_40MHZ 0

#define ID_ENABLE_PRINT_CID 1

#define MMMC_CMD_STATRT_BYTE 0
#define TINYUSB_RECIVE_BUFFER_SIZE 1024
#define MMC_DATA_CMD_FREAM_LAST_SIZE 5
#define MMC_DATA_CMD_BASIC_FREAM_SIZ 4
#define RECIVE_BUFFER_SIZE_M 64

#define MMMC_CMD_REQUEST_FREAM 10
#define MMMC_CMD_FREAM_START_BYTE 0
#define MMMC_CMD_FREAM_END_BYTE 0x7A
#define MMC_CMD_RESPONCE_FREAM_ENDBIT 0XD9D8D3C3
#define IS_ENABLE_MMC_FUNCTION_DEBUG 1
#define MMC_CMD_FREAM_RWFLAG_WRITE 0X02
#define MMC_CMD_FREAM_RWFLAG_READ 0X01
#define MMC_PESP_RETURN_VALUE_ARRY_ZEARO 0
#define MMC_REQUST_AND_RESP_DEFALUT_VALUE 0
#define MMC_CMD_FREAM_ERR_RESP_VALUE 0XFF
#define MMC_CMD_FREAM_ERR_RESP_VALUE_SIZE 16

#define MMC_CMD_Vrify_YX3126 0X03
#define MMC_CMD_WRITE_RWFLAGE 0X02
#define MMC_CMD_DEBUG_FLAG 0X09
#define MMC_CMD_READ_RWFLAGE 0X01
#define MMC_CMD_BUS_TEST_FLAG 0X04
#define MMC_CMD_PROGRAM_CID_CSD_FLAG 0X06

#define MMC_DATA_CMD_FREAM_START_BYTE 0XBA
#define MMC_DATA_CMD_FREAM_END_BYTE 0XAB

#define MMC_DATA_CMD_FREAM_TANSFER_SLAVE_TO_HOST_RWFLAG 0X03
#define MMC_DATA_CMD_FREAM_BASIC_SIZE 8 /// SEE  Frame definition

#define MMC_DATA_CMD_FREAM_TANSFER_HOST_TO_SLAVE_FLAG 0X00
#define MMC_DATA_CMD_FREAM_TANSFER_SLAVE_TO_HOST_FLAG 0X01
#define MMC_DATA_CMD_FREAM_WFLAGE 0X02 /// WRITE 2
#define MMC_DATA_CMD_FREAM_RFLAGE 0X01 /// READ 1

#define MMC_DATA_CMD_FREAM_ENDMARK_ONEBYTE 0XAA
#define MMC_DATA_CMD_FREAM_ENDMARK_TWOBYTE 0XBB
#define MMC_DATA_CMD_FREAM_ENDMARK_THREEBYTE 0XCC
#define MMC_DATA_CMD_FREAM_ENDMARK_FOURBYTE 0XDD

#define MMC_OPNE_BUTTON_FREAM 12
#define MMC_OPNE_BUTTON_FREAM_FIRST_BYTE 0X6D
#define MMC_OPNE_BUTTON_FREAM_SCOND_BYTE 0X6D
#define MMC_OPNE_BUTTON_FREAM_LAST_BUTONE_BYTE 0X0D
#define MMC_OPNE_BUTTON_FREAM_LAST_BYTE 0X0A

#define MMC_CLOSE_BUTTON_FREAM 6
#define MMC_CLOSE_BUTTON_FREAM_FIRST_BYTE 0X72
#define MMC_CLOSE_BUTTON_FREAM_SCOND_BYTE 0X65
#define MMC_CLOSE_BUTTON_FREAM_LAST_BUTONE_BYTE 0X75
#define MMC_CLOSE_BUTTON_FREAM_LAST_BYTE 0X6E

#define IS_ENABLE_CMD_DATA_DEBUG 1
#define MMC_TRUE 1
#define MMC_FALSE 0

#define START_VIRGIN_MASSAGECMD_FLAG 1
#define VIRGIN_TRIUMPH_FLAG 0x5AA5

#define FACTORY_MODE_FREAM_SIZE 10
#define FACTORY_MODE_FREAM_START_BIT 0xAABBCCDD
#define FACTORY_MODE_FREAM_END_BIT 0xDDBBCCAA
#define IS_ENBALE_FactoryModeProcess 1
#define BUS_TEST_DATA_SIZE 8
#define BUS_TEST_DATA_START_FLAG 0x80400BA
#define BUS_TEST_DATA_GET_VALID_BYTE 2

#define LOG_BUS_WITH_8BIT 3
#define LOG_BUS_WITH_4BIT 2
#define LOG_BUS_WITH_1BIT 0

#define BUS_WITH_8BIT 8
#define BUS_WITH_4BIT 4
#define BUS_WITH_1BIT 1
#define IS_ENABLE_SEND_CMD_CSD 0 /// is send cmd9

// 0X03b70000      1bit
// 0X03b70100      4bit
// 0X03b70200      8 bit

#define FACTORY_MODE_Process_FREAM_SIZE 10
#define FACTORY_MODE_Process_FREAM_START_BIT 0xDDBBCCAA
#define FACTORY_MODE_Process_FREAM_END_BIT 0xAABBCCDD
#define RCA_HARDVALUE 0x00010000

#define IS_ENABLE_CHECKA_STATUS 0
#define SDMMC_READY_FOR_DATA_TIMEOUT_US_FREAM (5 * 1000 * 1000) /// 6microseconds
#define MMC_TRANSFER_STATE 0x00000900
#define MMC_TSEND_STATE_TIMEOUT 10000
#define IS_SURE_MMC_READ_FUNCTION 1
#define IS_ENBAEL_ZYP_DEBUG_MMC 1

#define VERIFY_EMMC_CASE 1

#if VERIFY_EMMC_CASE
#define MMC_QUEUED_TASK_PARAMS 44
#define MMC_QUEUED_TASK_ADDRESS 45
#define MMC_EXECUTE_READ_TASK 46
#define MMC_EXECUTE_WRITE_TASK 47
#define MMC_CMDQ_TASK_MGMT 48
#define EXT_CSD_CMDQ_MODE_EN 15

#define MMC_PACKED_VERSION 0x00000000
#define MMC_PACKED_W_FLAGS 0x00000002
#define MMC_PACKED_R_FLAGS 0x00000001
#define MMC_PACKED_EVY_BLACNT_ADD_SHIF_BYTE 8
#endif

#define MMC_LOCK_UNLOCK 42

///EXT_CSD

#define EXT_CSD_RST_N_FUNCTION   162 /* R/W */
#define USER_WP                  171
#define BOOT_WP                  173
#define BOOT_WP_STATUS           174
#define PARTITION_CONFIG         179

#define EXT_CSD_SUPPORTED_MODES		493	/* RO */
#define EXT_CSD_FFU_FEATURES		492	/* RO */
#define EXT_CSD_FFU_ARG_3		490	/* RO */
#define EXT_CSD_FFU_ARG_2		489	/* RO */
#define EXT_CSD_FFU_ARG_1		488	/* RO */
#define EXT_CSD_FFU_ARG_0		487	/* RO */
#define EXT_CSD_CMDQ_SUPPORT		308	/* RO */
#define EXT_CSD_NUM_OF_FW_SEC_PROG_3	305	/* RO */
#define EXT_CSD_NUM_OF_FW_SEC_PROG_2	304	/* RO */
#define EXT_CSD_NUM_OF_FW_SEC_PROG_1	303	/* RO */
#define EXT_CSD_NUM_OF_FW_SEC_PROG_0	302	/* RO */
#define EXT_CSD_FW_CONFIG		169	/* R/W */

#define EXT_CSD_DATA_SECTOR_SIZE	61 /* R */
#define EXT_CSD_MODE_CONFIG		30
#define EXT_CSD_MODE_OPERATION_CODES	29	/* W   0x01 FFU_INSTALL  0x02 FFU_ABORT*/     
#define EXT_CSD_FFU_STATUS		26	/* R */


#pragma pack(1)
struct RequestBuffer
{                  /// recive host computer cmd frame  10byte
    char startBit; ///
    char requestDirection;         /// 0  nomal handle(open_end)    1  deiend_end
    char cmdId;
    unsigned int cmdParm;
    char rwFlag;
    char respType;
    char endBit;
};
#pragma pack()
#pragma pack(1)
struct ResponseBuffer
{ /// salva computer myself construct data send host computer
    char startBit;
    char requestDirection;
    char cmdId;
    char rwFlag;
    char respType;
    // unsigned int  responseParm;
    unsigned int responseParm[4];
    // char endBit;
    unsigned int endBit;
};
#pragma pack()
#pragma pack(1)
struct RwReciveBuffer
{ /// recive host computer data frame
    char startBit;
    char requestDirection;
    char rwFlag;
    char dataLength; /// max 127 7F，should be unsigned char 255    FF
    // char data[MMC_MAX_BLOCK_LEN*1024];
    unsigned char *data; /// 8bit 64位
    char endBit;
    // char other;
};
#pragma pack()

#pragma pack(1)
typedef struct FactoryMode
{
    unsigned int factory_mode_starbit;
    // uint8_t factory_mode_err_message[2];
    uint16_t factory_mode_err_message_flage;
    unsigned int factory_mode_endbit;
} FactoryMode_T;
#pragma pack()

#if IS_ENBALE_FactoryModeProcess
#pragma pack(1)
typedef struct FactoryModeProcess
{
    unsigned int factoryModeProcessStarbit;
    uint16_t factoryModeProcessData;
    unsigned int factoryModeProcessEndbit;
} FactoryModeProcess_T;
#pragma pack()

#pragma pack(1)
typedef struct MMC_LOCK
{
    uint8_t mode;
    uint8_t pwd;
    uint8_t len;
} MMC_LOCK_T;
#pragma pack()

typedef struct MMC_FORCE_CLEAN
{
    uint8_t mode;
} MMC_FORCE_CLEAN_T;

typedef enum
{
    THE_FIRST_CMD61,
    THE_SECOND_CMD61,
    THE_INIT_GO_IDLE,
    THE_INIT_OP_COND,
    THE_INTI_CID,
    THE_INIT_SET_RELATIVE,
    THE_INIT_SEND_CSD,
    THE_INIT_SLECTE_CARD,
    THE_INIT_READ_EXTCSD,
    THE_CMD60_PRAME1,
    THE_CMD60_PRAME2,
    THE_WRITE_LLF,
    THE_VCMD60_DEPLOY_EXEC,
    THE_VCMD60_DNLD_ADD,
    THE_VCMD60_DNLD_STARTADDR,
    THE_CMD13_WATING_MFG,
    THE_CMD6_FFU_MODE,
    THE_WRITE_LLF_ADDR,
    THE_CMD6_FFU_INSTALL,
    THE_CMD6_CONFIG_PARM_FFU_MODE,
    THE_WRITE_MAIN_ADDR,
    THE_LAST_CMD6_PARM_INSTALL,
    THE_CMD6_SDR_8BIT_MODE, /// todo use
} Factory_mode_process_state_e;
#endif
typedef enum
{
    THE_FIRST_CMD61_ERR,
    THE_SECOND_CMD61_ERR,
    THE_INIT_GO_IDLE_ERR,
    THE_INIT_OP_COND_ERR,
    THE_INTI_CID_ERR,
    THE_INIT_SET_RELATIVE_ERR,
    THE_INIT_SEND_CSD_ERR,
    THE_INIT_SLECTE_CARD_ERR,
    THE_INIT_READ_EXTCSD_ERR,
    THE_CMD60_PRAME1_ERR,
    THE_CMD60_PRAME2_ERR,
    THE_WRITE_LLF_ERR,
    THE_VCMD60_DEPLOY_EXEC_ERR,
    THE_VCMD60_DNLD_ADDR_ERR,
    THE_VCMD60_DNLD_STARTADDR_ERR,
    THE_CMD13_WATING_MFG_ERR,
    THE_CMD6_FFU_MODE_ERR,
    THE_WRITE_LLF_ADDR_ERR,
    THE_CMD6_FFU_INSTALL_ERR,
    THE_CMD6_CONFIG_PARM_FFU_MODE_ERR,
    THE_WRITE_MAIN_ADDR_ERR,
    THE_LAST_CMD6_PARM_INSTALL_ERR,
    THE_CMD6_SDR_8BIT_MODE_ERR,
    THE_VIRGIN_FLOW_SUCCESS = 100,
} Factory_mode_err_state_e;

enum{
    FFU_POWER_MODE,
    FFU_CMD0_MODE,
    FFU_HARDRESET_MODE,
    FFU_SMI_WRITE_RANDOME_DATA_MODE,
    FFU_CMD25_PARME_ERR_MODE,
};

extern unsigned int readMultipeCount;
extern struct ResponseBuffer responsebuffer;
extern struct RequestBuffer requestbuffer;
extern struct RwReciveBuffer rwRecivebuffer;
extern FactoryMode_T factory_mode_t;
#if IS_ENBALE_FactoryModeProcess
extern FactoryModeProcess_T factory_mode_process_t;
#endif
extern sdmmc_card_t *card;
extern sdmmc_command_t *cmd;
extern uint8_t errmsage;
extern unsigned int writeMultipeCount;
extern sdmmc_slot_config_t slot_config;
extern bool is_power_off;
// extern bool  is_emmc_init;

void EMMC_GPIO_init(size_t gpio_iomux_in);
void EMMC_POWEROF(size_t gpio_iomux_in);
void EMMC_POWERON(size_t gpio_iomux_in);
void EMMC_POWER_SET(size_t gpio_iomux_in, int value);

esp_err_t zyp_process_protect_command(struct RwReciveBuffer *rwRecivebuffer,sdmmc_card_t *card,struct RequestBuffer *requestbuffer,struct ResponseBuffer *responsebuffer);
esp_err_t zyp_mmc_clear_emmc(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responseBuffer, MMC_FORCE_CLEAN_T *force_clean, uint8_t clean_totalbyte);
esp_err_t zyp_mmc_lock_unlock(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responseBuffer, MMC_LOCK_T *lock, uint8_t write_totalbyte);
esp_err_t zyp_mmc_create_cmdq_task_read(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint taskId, uint number_block, uint lba);
esp_err_t zyp_mmc_create_cmdq_task_write(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint taskId, uint number_block, uint lba);
esp_err_t zyp_mmc_create_task_read(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint8_t taskdId, uint8_t tm_op_code);
esp_err_t zyp_mmc_packed_read(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint *addresses, uint *block_counts, uint num_entries);
esp_err_t zyp_mmc_packed_write(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, size_t *address, size_t *block_counts, size_t num_entries, size_t *write_data);
esp_err_t zyp_mmc_old_erase_group(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, size_t write_block_cnt);
esp_err_t zyp_boot1_add_perm_wp(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
esp_err_t zyp_write_X_partion_1MB(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
esp_err_t zyp_write_X_partion_custom_block(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,
    size_t total_blocks,size_t chunk_blocks,size_t buffer_size);
void zyp_read_cid(sdmmc_card_t *card);

esp_err_t zyp_ffu_download_general(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,uint8_t *ext_csd_buffer);
esp_err_t  zyp_ffu_download_powercycle(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
esp_err_t  zyp_ffu_download_cmd0(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,uint8_t *ext_csd_buffer);
esp_err_t  zyp_ffu_download_hardreset(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,uint8_t *ext_csd_buffer);
esp_err_t  zyp_ffu_download_random_write(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,uint8_t *ext_csd_buffer);
esp_err_t  zyp_ffu_download_cmd25_parm_err(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer,uint8_t *ext_csd_buffer);


esp_err_t zyp_handle_cmd_fream(struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, sdmmc_card_t *card, sdmmc_command_t *cmd);
esp_err_t zyp_mmc_erase_sectors(sdmmc_card_t* card, size_t start_sector,size_t sector_count, sdmmc_erase_arg_t arg);
esp_err_t zyp_sdmmc_host_init_config_slots(int slot, const sdmmc_slot_config_t *slot_config);
esp_err_t zyp_sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t *slot_config);
esp_err_t zyp_debugbin_flow(sdmmc_card_t *card, uint32_t fileIndex);
FactoryMode_T zyp_construct_vriginal_message_fream(FactoryMode_T factory_mode_t);
#if IS_ENBALE_FactoryModeProcess
FactoryModeProcess_T *zyp_construct_vriginal_process_message_fream(FactoryModeProcess_T *factory_mode_process_t, Factory_mode_process_state_e process_state);
void zyp_construct_vriginal_process_message_fream_to_send(FactoryModeProcess_T *factory_mode_process_t, Factory_mode_process_state_e process_state, uint8_t cmd, unsigned int args);
#endif

esp_err_t zyp_sendCmdPro_CIDOrCSD_RespCmdFrame(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, struct RwReciveBuffer *rwRecivebuffer);

esp_err_t zyp_sendCmd56W(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct RwReciveBuffer *rwRecivebuffer, size_t data_cmd_cnt);
esp_err_t zyp_sendCmd56R(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, size_t start_block, size_t block_count);

esp_err_t zyp_sendCmd19AndRespFrame(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
esp_err_t zyp_sendCmd14AndRespFrame(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
void zyp_wrappeRespnceDataFrameAndSendHex_nostruct(struct RequestBuffer *requestbuffer, char *sendReadDataCmdbuffer, unsigned int sendReadDataCmdbufferSize);

#if IS_SURE_MMC_READ_FUNCTION
esp_err_t zyp_sendReadCmd(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, unsigned int *sendReadDataCmdbuffer, size_t start_block, size_t block_count);
#endif
void zyp_reverse_cid_bytes(const uint32_t *response, uint8_t *cid);
int zyp_setBlockCountCmd(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer);
void zyp_wrappeRespnceCmdR_W_Err_FrameAndSendHex(sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
void zyp_wrappeRespnceCmdFrameAndSendHex(sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
esp_err_t zyp_sendNomalCmd(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer);
esp_err_t zyp_judge_is_nomal_cmd(sdmmc_command_t *cmd);
void zyp_getrespType(struct RequestBuffer *requestbuffer, sdmmc_command_t *cmd);
void zyp_getrequetCmdID(struct RequestBuffer *requestbuffer, sdmmc_command_t *cmd);
esp_err_t zyp_mmc_set_blocklen_cmd_fream(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer);
esp_err_t zyp_sdmmc_init_mmc_read_ext_csd(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint8_t *ext_csd);
esp_err_t zyp_sendCmd_EXT_CSD_RespData(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer);
void zyp_CmdStatus_wrappeRespnceDataFrameAndSendHex_nostruct(struct RequestBuffer *requestbuffer, uint8_t *sendReadDataCmdbuffer, unsigned int sendReadDataCmdbufferSize);
esp_err_t zyp_sdmmc_send_cmd_set_relative_addr_handlefream(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, uint16_t *out_rca);
esp_err_t zyp_sdmmc_send_cmd_send_csd_handlefream(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, sdmmc_csd_t *out_csd);
void zyp_handle_mmc_cmd_response(sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, bool is_error);

esp_err_t zyp_sendWriteCmd(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, struct RwReciveBuffer *rwRecivebuffer, size_t data_cmd_cnt);

esp_err_t zyp_mmc_write_sectors(sdmmc_command_t *cmd, sdmmc_card_t *card, struct RequestBuffer *requestbuffer, const void *src,
                                size_t start_block, size_t block_count);
esp_err_t zyp_mmc_write_sectors_dma(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, const void *src,
                                    size_t start_block, size_t block_count);
esp_err_t zyp_mmc_send_cmd_send_status(sdmmc_card_t *card, sdmmc_command_t *cmd, uint32_t *out_status);

void zyp_sdmmc_card_print_info(FILE *stream, const sdmmc_card_t *card);
static esp_err_t zyp_init_sdmmc_host(int slot, const void *slot_config, int *out_slot);
esp_err_t zyp_host_emmc_initialize_only(const sdmmc_host_t *host_config, const void *slot_config, sdmmc_card_t **out_card);
esp_err_t zyp_sdmmc_card_init(const sdmmc_host_t *config, sdmmc_card_t *card);
esp_err_t zyp_cmd61(sdmmc_card_t *card);
esp_err_t zyp_init_mmc(const sdmmc_host_t *config, sdmmc_card_t *card);

esp_err_t zyp_host_initialize_only(const sdmmc_host_t *host_config, const void *slot_config, sdmmc_card_t **card, sdmmc_command_t **cmd);
esp_err_t zyp_detect_emmc(const sdmmc_host_t *host_config, sdmmc_card_t *out_card);
esp_err_t zyp_sdmmc_configure_bus_fre_mode(sdmmc_card_t *card, uint32_t freq_khz, int bus_width, bool use_ddr);

esp_err_t zyp_init_mmc_random(sdmmc_card_t *card);

esp_err_t zyp_send_cmd60_cmd61_random(sdmmc_card_t *card, uint32_t value, uint8_t cmd_index); /// if take out ;  err is not complex
esp_err_t zyp_cmd0_go_idle_state(sdmmc_card_t *card);
esp_err_t zyp_mmc_send_cmd_send_op_cond(sdmmc_card_t *card);
esp_err_t zyp_sdmmc_init_cid(sdmmc_card_t *card);
esp_err_t zyp_sdmmc_send_cmd_set_relative_addr(sdmmc_card_t *card, uint16_t *out_rca);
esp_err_t zyp_mmc_send_cmd_select_card(sdmmc_card_t *card, uint32_t rca);

esp_err_t zyp_mmc_send_status(sdmmc_card_t *card, uint32_t *out_status);
esp_err_t zyp_sdmmc_send_cmd_send_csd(sdmmc_card_t *card, sdmmc_csd_t *out_csd);
esp_err_t zyp_switch_cmd(sdmmc_card_t *card, uint32_t value);
esp_err_t zyp_sdmmc_switch_cmd_(sdmmc_card_t *card, uint8_t set, uint8_t index, uint8_t value);
esp_err_t zyp_mmc_set_blocklen(sdmmc_card_t *card);
esp_err_t zyp_mmc_set_blocklcnt(sdmmc_command_t *cmd, sdmmc_card_t *card);

esp_err_t zyp_mmc_random_write_read(sdmmc_card_t *card);
esp_err_t zyp_write_sector_sdmmc(size_t sector_size,
                                 size_t addr,
                                 uint32_t lba,
                                 uint32_t offset,
                                 size_t size,
                                 const void *src);

esp_err_t zyp_write_psram_to_emmc(sdmmc_card_t *card, uint8_t *psram_addr, size_t data_size, size_t start_block);

esp_err_t zyp_virgin_flow(sdmmc_card_t *card, uint32_t fileIndex);

esp_err_t zyp_mmc_hanlde_write_protect_or_type(sdmmc_card_t *card, sdmmc_command_t *cmd, struct RequestBuffer *requestbuffer, struct ResponseBuffer *responsebuffer, uint8_t *read_bufer,uint8_t read_size);


#endif